home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 12984 / 12984.xpi / chrome / VideoDownloaderToolbar.jar / content / sites.js < prev    next >
Text File  |  2010-01-29  |  3KB  |  122 lines

  1. if(!com) var com={};
  2. if(!com.VidBar) com.VidBar={};
  3.  
  4. com.VidBar.VidSites = function() {
  5. }
  6.  
  7. com.VidBar.VidSites.prototype = {
  8.     db : null,
  9.  
  10.     getTopWindow : function() {
  11.         var wwatch = Components.classes["@mozilla.org/embedcomp/window-watcher;1"]
  12.                 .getService()
  13.                 .QueryInterface(Components.interfaces.nsIWindowWatcher);
  14.         var i = wwatch.getWindowEnumerator();
  15.         while (i.hasMoreElements()) {
  16.             var w = i.getNext().QueryInterface(Components.interfaces.nsIDOMWindow);
  17.             try {
  18.                 var w0 = w
  19.                         .QueryInterface(Components.interfaces.nsIDOMWindowInternal);
  20.                 if (w0.location.href == "chrome://browser/content/browser.xul")
  21.                     return w0;
  22.             } catch (e) {
  23.             }
  24.         }
  25.         return null;
  26.     },
  27.  
  28.     onLoad : function(event) {
  29.         this.db = new com.VidBar.VidDB();
  30.         this.loadSites();
  31.         var _this = this;
  32.         this.clickListener = function() {
  33.             _this.checkSite();
  34.         }
  35.         var t = document.getElementById("vidbar-media-sites");
  36.         t.addEventListener("dblclick", this.clickListener, true);
  37.     },
  38.     onUnload : function(event) {
  39.         var t = document.getElementById("vidbar-media-sites");
  40.         t.removeEventListener("dblclick", this.clickListener, true);
  41.     },
  42.     loadSites:function(){
  43.         var children = document.getElementById("siteChildren");
  44.         while(children.firstChild)
  45.             children.removeChild(children.firstChild);
  46.             
  47.         var sites = this.db.getSites();
  48.         for(var i=0;i<sites.length;i++){
  49.             var name = sites[i].name;
  50.             var url = sites[i].url;
  51.             this.appendItem(name, url);
  52.         }
  53.     },
  54.     appendItem:function(name, url){
  55.         var children = document.getElementById("siteChildren");
  56.         var    item = document.createElement("treeitem");
  57.         item.setAttribute("siteUrl", url);
  58.         var row = document.createElement("treerow");
  59.         var cell = document.createElement("treecell");
  60.         cell.setAttribute("label", name);
  61.         row.appendChild(cell);
  62.         item.appendChild(row);
  63.         children.appendChild(item);
  64.     },
  65.     add : function() {
  66.         var name = VidUtils.trimString(document.getElementById("new-site-name").value);
  67.         var url = VidUtils.trimString(document.getElementById("new-site-url").value);
  68.         
  69.         if(!name){
  70.             alert("Please enter the new site name.");
  71.             return;
  72.         }
  73.         if(!url){
  74.             alert("Please enter the new site URL.");
  75.             return;
  76.         }
  77.         
  78.         var sites = this.db.getSites();
  79.         for(var i=0;i<sites.length;i++){
  80.             if(sites[i].name==name){
  81.                 alert("Site "+name+" does exist.");
  82.                 return;
  83.             }
  84.         }
  85.         
  86.         this.db.addSite(name, url);
  87.         this.appendItem(name, url);
  88.         
  89.         document.getElementById("new-site-name").value= "";
  90.         document.getElementById("new-site-url").value= "";
  91.     },
  92.     remove : function() {
  93.         var t = document.getElementById("vidbar-media-sites");
  94.         var item = t.contentView.getItemAtIndex(t.currentIndex);
  95.         if(item){
  96.             var name = item.firstChild.firstChild.getAttribute("label");
  97.             if(confirm("Do you want to delete site "+name+"?")){
  98.                 this.db.removeSite(name);
  99.                 item.parentNode.removeChild(item);
  100.             }
  101.         }
  102.     },
  103.     restore:function(){
  104.         this.db.restoreDefaultSites();
  105.         this.loadSites();
  106.     },
  107.     checkSite : function() {
  108.         var t = document.getElementById("vidbar-media-sites");
  109.         var siteUrl = t.contentView.getItemAtIndex(t.currentIndex)
  110.                 .getAttribute("siteUrl");
  111.         // alert(siteUrl);
  112.         var top = this.getTopWindow();
  113.         if (top) {
  114.             var browser = top.getBrowser();
  115.             var tab = browser.addTab(siteUrl);
  116.             setTimeout(function(b, t) {
  117.                         b.selectedTab = t;
  118.                     }, 0, browser, tab);
  119.         }
  120.     }
  121. };
  122.